<?php
require_once('C:\xampp\htdocs\Vote4U\dataBase.php');
	if(isset($_POST['submit'])){
		require_once('conn.php');
		$empty = 'You can\'t leave this empty';
		$allValid = true;
		$errorMessage = array();
		$qfid = $_GET['id'];
		$questionArray = mysql_query("select qid, type from question where qfid = $qfid");
		while($question = mysql_fetch_assoc($questionArray)){
			$qid = $question['qid'];
                        if(dataBase::errorFlag($qid)){
                        //if(!isset($_POST[$qid]) || ($_POST[$qid] == '')){
				$errorMessage[$qid] = $empty;
				$allValid = false;
			}
		}
		if(!$allValid){
			session_start();
			$_SESSION['previousAnswer'] = $_POST;
			$_SESSION['errorMessage'] = $errorMessage;
		}
		else{
			mysql_data_seek($questionArray, 0);
			while($question = mysql_fetch_assoc($questionArray)){
				$qid=$question['qid'];
				$answer = $_POST[$qid];
				if($question['type'] == 1){
					mysql_query("update mc set vote=vote+1 where qid = $qid and choice = '$answer'");
				}
				else{
					mysql_query("insert into open(answer,qid) values('$answer',$qid)");
				}
			}
			header('location:index.php');
		}
	}
	
	if(isset($_SESSION['previousAnswer'])){
		$previousAnswer = $_SESSION['previousAnswer'];
		$errorMessage = array();
		foreach($_SESSION['errorMessage'] as $key => $value){
			$errorMessage[$key] = "<font style='color: red'>";
			$errorMessage[$key] .= $value;
			$errorMessage[$key] .= "</font>";
		}
		unset($_SESSION['previousAnswer']);
		unset($_SESSION['errorMessage']);
	}
	
	include_once('header.php');
	if(isset($_GET['id'])){
		$qfid = $_GET['id'];
		require('conn.php');
		$passwordQuery = mysql_query("select password from questionForm where qfid = $qfid");
		if($password = mysql_fetch_assoc($passwordQuery)){
			if($password['password'] == '' || (($password['password'] != '') && isset($_POST['checkPassword']) && isset($_POST['password']) && ($_POST['password'] == $password['password']))){
				$questionArray = mysql_query("select qid, question,type from question where qfid = $qfid");
				if($question = mysql_fetch_assoc($questionArray)){
					echo "<form action='' method='post'>";
					$i = 1;
					do 
					{
						echo "Q".$i.": ".$question['question']."<br>";
						$qid = $question['qid'];
						if(isset($errorMessage[$qid]))
							echo $errorMessage[$qid]."<br>";
						if($question['type'] == 1){
							$choiceArray = mysql_query("select choice, qid from mc where qid = $qid");
							while($choice = mysql_fetch_assoc($choiceArray)){
								$select = '';
								if(isset($previousAnswer) && isset($previousAnswer[$qid])&& ($choice['choice'] == $previousAnswer[$qid]))
									$select = "checked='checked'";
								echo "<input type='radio' name='$qid' value='".$choice['choice']."' ".$select.">".$choice['choice']."<br>";
							}
							echo "<br>";
						}
						else{
							$value ='';
							if(isset($previousAnswer))
								$value = $previousAnswer[$qid];
							echo "<input type='text' name='$qid' value='".$value."'><br><br>";
						}
						$i++;
					}while($question = mysql_fetch_assoc($questionArray));
					mysql_close($connect);
					echo "<input type='submit' name='submit' value='submit'>";
					echo "</form>";
				}
			}
			else{
				if(isset($_POST['checkPassword']) && isset($_POST['password'])){
					echo "<font color=red>";
					if($_POST['password'] == '')
						echo 'You can\'t leave this empty';
					else if($_POST['password'] != $password['password'])
						echo 'The password you entered is incorrect.';
					echo "</font>";
				}
				echo "<form action='' method='post'>";
				echo "Enter the password:<br>";
				echo "<input type='password' name='password'><br>";
				echo "<input type='submit' name='checkPassword' value='Enter'>";
				echo "</form>";
			}
		}
		else{
			echo "This Question Form does not exist.";
		}
	}
	else{
		echo "Invalid access!";
	}
?>
